<template>
  <div>
    子组件
    <el-tree
      ref="perTree"
      :data="list"
      :props="{label: 'name'}"
      default-expand-all
      show-checkbox
      check-strictly
      node-key="id"
    />
    <div>
      <el-button @click="doSave">确定</el-button>
      <el-button @click="$emit('assignPerSuccess')">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permissions'
import { arrayToTree } from '@/utils'
import { getRoleDetail, savePermission } from '@/api/settings'
export default {
  props: {
    id: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
    this.loadRolePermission()
  },
  // methods:事件处理函数
  methods: {
    // 加载权限列表
    async loadPermissionList() {
      const res = await getPermissionList()
      this.list = arrayToTree(res.data.data, '0')
    },
    // 加载被点击角色的权限列表
    async loadRolePermission() {
      const res = await getRoleDetail(this.id)
      this.$refs.perTree.setCheckedKeys(res.data.data.permIds)
    },
    // 更新权限
    async doSave() {
      // 获取被选取的权限点
      const permIds = this.$refs.perTree.getCheckedKeys()
      // 发送ajax更新权限
      await savePermission({ id: this.id, permIds: permIds })
      this.$emit('assignPerSuccess')
    }
  }
}
</script>

<style>

</style>
